package com.jyskebank.android.toggl.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.jyskkebank.android.toggl.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;
	
	private int[] groupColors = new int[] { 0xE099C68E , 0x30B1FB17};

	private ArrayList<String> groups;

	private ArrayList<ArrayList<TimeEntry>> children;

	public ExpandableListAdapter(Context context, ArrayList<String> groups,
			ArrayList<ArrayList<TimeEntry>> children) {
		this.context = context;
		this.groups = groups;
		this.children = children;
	}

	/**
	 * A general add method, that allows you to add a Vehicle to this list
	 * 
	 * Depending on if the category opf the vehicle is present or not, the
	 * corresponding item will either be added to an existing group if it
	 * exists, else the group will be created and then the item will be added
	 * 
	 * @param vehicle
	 */
	public void addItem(TimeEntry timeEntry) {
		if (!groups.contains(timeEntry.getStartTime())) {
			groups.add(timeEntry.getStartTime());
		}
		System.out.println("groups :" + groups);
		//System.out.println("TimeEntry : " + timeEntry.getStartTime().substring(0,timeEntry.getStartTime().indexOf("T")));
		int index = groups.indexOf(timeEntry.getStartTime());
		if (children.size() < index + 1) {
			children.add(new ArrayList<TimeEntry>());
		}
		children.get(index).add(timeEntry);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// Return a child view. You can load your custom layout here.
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TimeEntry timeEntry = (TimeEntry) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		tv.setText("   " + timeEntry.getDescription() + " "+ formatIntoHHMMSS(timeEntry.getDuration()));
		
		// Depending upon the child type, set the imageTextView01
		tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
/*		if (vehicle instanceof Car) {
			tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
		} else if (vehicle instanceof Bus) {
			tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0);
		} else if (vehicle instanceof Bike) {
			tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0);
		}
*/		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// Return a group view. You can load your custom layout here.
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
		tv.setBackgroundColor(groupColors[groupPosition%2]);
		ArrayList<TimeEntry> childrenList = this.children.get(groupPosition);
		long duration = 0;
		for(TimeEntry timeEntry : childrenList){
			duration = duration+ timeEntry.getDuration();
		}
		tv.setText("     " + formatGroup(group,duration));
		return convertView;
	}

	private CharSequence formatGroup(String group, long duration) {
		String df = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(df);
		SimpleDateFormat newFormat = new SimpleDateFormat("d MMM, EEE");
		Date ydate = null;
		try {
			ydate = sdf.parse(group);
		} catch (ParseException e) {
			e.printStackTrace();
			return group;
		}
		String formatIntoHHMMSS = formatIntoHHMMSS(duration);
		return newFormat.format(ydate)+" ("+formatIntoHHMMSS+"h)";
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	private  String formatIntoHHMMSS(long secsIn){

		int hours = (int) (secsIn / 3600),
		remainder = (int) (secsIn % 3600),
		minutes = remainder / 60;
		//seconds = remainder % 60;
	
		return ( (hours < 10 ? "0" : "") + hours
		+ ":" + (minutes < 10 ? "0" : "") + minutes);
		//+ ":" + (seconds< 10 ? "0" : "") + seconds );

	}

}
